package com.longteng.diamond.util;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created with IntelliJ IDEA.
 * User: taki
 * Date: 14-2-14
 * Time: 下午4:53
 * 获取当前日期
 * To change this template use File | Settings | File Templates.
 */
public class DateUtil {

    /**
     * 获取当前时间
     * @return
     */
    public String getCurrentDate() {
        /**设置日期格式**/
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = df.format(new Date());
        return d;
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getDate() {
        //设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String d = df.format(new Date());
        return d;
    }

    /**
     * 获取当前时间
     * @return
     */
    public static String getCurrentTime() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        String d = df.format(new Date());
        return d;
    }


    /**
     * 获取当前时间
     * @return
     */
    public static Date getCurrentTimeofDate() {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            String dt = df.format(new Date());
            return df.parse(dt);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 获取当前是第几周
     * @return null
     */
    public int getCurrentWeek() {
        SimpleDateFormat df = this.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        Date date = null;
        String create_time = df.format(new Date());
        try {
            date = df.parse(create_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int weekFlag = calendar.get(Calendar.WEEK_OF_YEAR);
        return weekFlag;
    }

    private SimpleDateFormat simpleDateFormat(String pattern){
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return df;
    }
    /**
     * 获取当前是本月的第几周
     * @return null
     */
    public int getCurrentWeekOfMonth() {
        SimpleDateFormat df = this.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
        Calendar calendar = Calendar.getInstance();
        calendar.setFirstDayOfWeek(Calendar.MONDAY);
        Date date = null;
        String create_time = df.format(new Date());
        try {
            date = df.parse(create_time);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        calendar.setTime(date);
        int weekFlag = calendar.get(Calendar.WEEK_OF_YEAR);
        return weekFlag;
    }

    /**
     * 转换日期格式
     * @return null
     */
    public static String parseDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date dt = new Date();
        try {
            dt = sdf.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return sdf.format(dt);
    }


    /**
     * 转换日期格式
     * @return null
     */
    public static Date parseStringToDate(String date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        Date dt = new Date();
        try {
            dt = sdf.parse(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dt;
    }

    /**
     * 转换日期格式
     * @return
     */
    public static String formatDate(Date date, String format) {
        SimpleDateFormat sdf = new SimpleDateFormat(format);
        try {
            return sdf.format(date);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return "";
    }


    /**
     * 描述:获取上个月的最后一天.
     * @return
     */
    public static String getLastMaxMonthDate(Date date) {
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd 23:59:59");
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        calendar.add(Calendar.MONTH, -1);
        calendar.set(Calendar.DAY_OF_MONTH, calendar.getActualMaximum(Calendar.DAY_OF_MONTH));
        return dft.format(calendar.getTime());
    }

    /**
     * 获取上一个月
     * @return
     */
    public static String getLastMonth(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(cal.MONTH, -1);
        SimpleDateFormat dft = new SimpleDateFormat("yyyy-MM-dd 00:00:00");
        cal.set(Calendar.DAY_OF_MONTH, cal.getActualMinimum(Calendar.DAY_OF_MONTH));
        String lastMonth = dft.format(cal.getTime());
        return lastMonth;
    }


    /**
     * 时间相减
     * @param date
     * @param month
     * @param day
     * @return
     */
    public static Date getDiffTime(Date date, int month, int day) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        cal.add(Calendar.MONTH, month);
        cal.add(Calendar.DATE, day);
        return cal.getTime();
    }
    /**
     * 获取英文星期
     * @param n
     * @return
     */
    public static String getEnWeekByNum(int n) {
        String e = "";
        switch (n) {
            case 1:
                e="MON";
                break;
            case 2:
                e="TUE";
                break;
            case 3:
                e="WED";
                break;
            case 4:
                e="THU";
                break;
            case 5:
                e="FRI";
                break;
            case 6:
                e="SAT";
                break;
            case 7:
                e="SUN";
                break;
        }
        return e;
    }
}
